package com.shujiaotong.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.shujiaotong.common.R;
import com.shujiaotong.mapper.NoticeMapper;
import com.shujiaotong.pojo.Category;
import com.shujiaotong.pojo.Notice;
import com.shujiaotong.service.INoticeService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author author
 * @since 2024-10-20
 */
@RestController
@RequestMapping("/notice")
@Slf4j
public class NoticeController {

    @Autowired
    private INoticeService noticeService;
    @Autowired
    private NoticeMapper noticeMapper;

    //分页
    @RequestMapping("page")
    public Object page(@RequestParam int page,
                       @RequestParam int pageSize,
                       @RequestParam(required = false,value = "name") String name){
//        接收参数
        log.info("当前页:{},页条数:{},活动:{}",page,pageSize,name);
//        分页条件
        Page<Notice> noticePage = new Page<>(page,pageSize);
//        查询条件
        LambdaQueryWrapper<Notice> qw = new LambdaQueryWrapper<>();
        qw.like(!StringUtils.isEmpty(name),Notice::getTitle,name);
//        分页查询
        noticeService.page(noticePage,qw);
//        返回值
        return R.success(noticePage);

    }

//    新增通知
    @PostMapping
    public R save(@RequestBody Notice notice){
        log.info("新增通知:{}",notice);
        noticeService.save(notice);
        return R.success("新增通知成功");
    }

//根据id查询
    @GetMapping("/{id}")
    public R getById(@PathVariable Long id ){
        log.info("根据id查询通知:{}",id);
        Notice notice = noticeService.getById(id);
        return R.success(notice);
    }

//    修改
    @PutMapping
    public R update(@RequestBody Notice notice){
        log.info("修改通知:{}",notice);
        noticeService.updateById(notice);
        return R.success("修改通知成功");
    }

//    删除通知
    @DeleteMapping
    public R delete(Long id){
        log.info("根据id删除通知:{}",id);
        noticeService.removeById(id);
        return R.success("删除通知成功");
    }

//    查询所有通知
    @GetMapping
    public R getAllNotices(){
        List<Notice> list = noticeService.list();
        return R.success(list);
    }
}
